package com.payment.ktb.activity.main4;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.payment.ktb.Interface.HttpCallbackListener;
import com.payment.ktb.R;
import com.payment.ktb.activity.BaseActivity;
import com.payment.ktb.adapter.CardWrapAdapter;
import com.payment.ktb.constants.HttpActions;
import com.payment.ktb.constants.HttpUrls;
import com.payment.ktb.model.VouchersEntity;
import com.payment.ktb.utils.AlertDialogUtils;
import com.payment.ktb.utils.HttpUtils;
import com.payment.ktb.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by admin on 2017/9/6.
 */

public class CardWrapActivity extends BaseActivity {


    @BindView(R.id.prlv_cardwrap)
    PullToRefreshListView prlv_cardwrap;
    @BindView(R.id.tv_cardwrap_watchexporecardwrap)
    TextView tv_cardwrap_watchexporecardwrap;

    private List<VouchersEntity> listDates = new ArrayList<>();
    CardWrapAdapter cardWrapAdapter;
    int page;

    boolean flagIsExpire = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cardwrap);
        ButterKnife.bind(this);
        initTitle("卡卷包");

        LinearLayout llBack = (LinearLayout) findViewById(R.id.ll_title_back);
        llBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (flagIsExpire) {
                    flagIsExpire = false;
                    tv_cardwrap_watchexporecardwrap.setVisibility(View.VISIBLE);
                    pullDownToRefresh();
                }else {
                    finish();
                }
            }
        });

        prlv_cardwrap.setMode(PullToRefreshBase.Mode.BOTH);
        prlv_cardwrap.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

                // 设置下拉刷新加载文本
                prlv_cardwrap.getLoadingLayoutProxy(true, false).setPullLabel("下拉刷新...");
                prlv_cardwrap.getLoadingLayoutProxy(true, false).setReleaseLabel("放开刷新...");
                prlv_cardwrap.getLoadingLayoutProxy(true, false).setRefreshingLabel("正在加载...");
                //下拉刷新
                pullDownToRefresh();
            }
            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                // 设置上拉加载文本
                prlv_cardwrap.getLoadingLayoutProxy(false, true).setPullLabel("上拉刷新...");
                prlv_cardwrap.getLoadingLayoutProxy(false, true).setReleaseLabel("放开刷新...");
                prlv_cardwrap.getLoadingLayoutProxy(false, true).setRefreshingLabel("正在加载...");
                pullUptoLoad();
            }
        });



        //1.data
        listDates = new Gson().fromJson(getIntent().getStringExtra("records"), new TypeToken<List<VouchersEntity>>() {}.getType());
        //2.adapter
        cardWrapAdapter = new CardWrapAdapter(mContext,listDates);
        //3.bind
        prlv_cardwrap.setAdapter(cardWrapAdapter);

    }


    //下拉刷新
    private void pullDownToRefresh() {

        progressDialogUtils.showProgress();
        //构造传输参数
        Map<String, String> map = new HashMap<String,String>();
        //0:未使用 1:已使用或过期
        if (flagIsExpire) {
            map.put("isUse","1");
        }else {
            map.put("isUse","0");
        }
        map.put("size","20");
        page = 0;
        map.put("page", String.valueOf(page));

        //向服务器发送请求
        HttpUtils.HttpRequestByVollery(HttpUrls.URL, HttpActions.UserVouchersList, map, mQueue, new HttpCallbackListener() {
            @Override
            public void onFinish(String response) {
                progressDialogUtils.dismissProgress();
                prlv_cardwrap.onRefreshComplete();
                try {
                    JSONObject jsonObject = new JSONObject(response);

                    //1.data
                    listDates = new Gson().fromJson(jsonObject.optString("records"), new TypeToken<List<VouchersEntity>>() {}.getType());
                    //2.adapter
                    cardWrapAdapter = new CardWrapAdapter(mContext,listDates);
                    //3.bind
                    prlv_cardwrap.setAdapter(cardWrapAdapter);
                    if (listDates == null || listDates.size() == 0) {
                        ToastUtils.showToast("没有更多数据啦！");
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                    AlertDialogUtils.showAlertDialog(mContext, getResources().getString(R.string.json_parse_error));
                }
            }

            @Override
            public void onError(VolleyError volleyError) {
                progressDialogUtils.dismissProgress();
                prlv_cardwrap.onRefreshComplete();
                AlertDialogUtils.showAlertDialog(mContext, volleyError.getMessage());
            }
        });
    }


    //上拉加载更多
    private void pullUptoLoad() {

        progressDialogUtils.showProgress();
        //构造传输参数
        Map<String, String> map = new HashMap<String,String>();
        //0:未使用 1:已使用或过期
        if (flagIsExpire) {
            map.put("isUse","1");
        }else {
            map.put("isUse","0");
        }
        map.put("size","20");
        page = page + 1;
        map.put("page", String.valueOf(page));

        //向服务器发送请求
        HttpUtils.HttpRequestByVollery(HttpUrls.URL, HttpActions.UserVouchersList, map, mQueue, new HttpCallbackListener() {
            @Override
            public void onFinish(String response) {
                progressDialogUtils.dismissProgress();
                prlv_cardwrap.onRefreshComplete();
                try {
                    JSONObject jsonObject = new JSONObject(response);

                    //1.data
                    List<VouchersEntity> temp = new Gson().fromJson(jsonObject.optString("records"), new TypeToken<List<VouchersEntity>>() {}.getType());
                    listDates.addAll(temp);
                    //2.adapter
                    cardWrapAdapter.notifyDataSetChanged();

                    if (temp == null || temp.size() == 0) {
                        ToastUtils.showToast("没有更多数据啦！");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    AlertDialogUtils.showAlertDialog(mContext, getResources().getString(R.string.json_parse_error));
                }
            }

            @Override
            public void onError(VolleyError volleyError) {
                progressDialogUtils.dismissProgress();
                prlv_cardwrap.onRefreshComplete();
                AlertDialogUtils.showAlertDialog(mContext, volleyError.getMessage());
            }
        });

    }





    @OnClick({R.id.tv_cardwrap_watchexporecardwrap})
    public void clickEvent(View view) {

        switch (view.getId()) {
            //查看过期或者使用过的卡卷
            case R.id.tv_cardwrap_watchexporecardwrap:
                flagIsExpire = true;
                tv_cardwrap_watchexporecardwrap.setVisibility(View.GONE);
                pullDownToRefresh();
                break;
        }
    }

}
